﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace YummyGame.Framework
{
    public class SkillComponent
    {
        private SkillOwner owner;
        private Dictionary<string, Skill> m_skills;

        public SkillComponent(SkillOwner owner)
        {
            m_skills = new Dictionary<string, Skill>();
            this.owner = owner;
        }

        public void Update()
        {
            foreach (var kv in m_skills)
            {
                kv.Value.Tick();
            }
        }
        public void LoadSkillFromJson(string path)
        {
            SkillPrototype protoType = SkillPrototypeLib.GetPrototype(path);
            Skill skill = protoType.GetSkill(owner);

            m_skills.Add(path, skill);
        }

        public bool CanUseSkill(string name)
        {
            Skill s = GetSkill(name);
            return s != null && s.CanUse();
        }

        public void UseSkill(string name)
        {
            if (!HasSkill(name)) return;
            Skill s = GetSkill(name);
            s.Use();
        }

        public bool HasSkill(string name)
        {
            return m_skills.ContainsKey(name);
        }

        public float GetVariable(string name, string key)
        {
            if (!HasSkill(name)) return -1;
            Skill skill = GetSkill(name);
            if (!skill.HasVariable(key)) return -1;
            return skill.GetVariable(key);
        }

        public bool SetVariable(string name, string key,float value)
        {
            if (!HasSkill(name)) return false;
            Skill skill = GetSkill(name);
            if (!skill.HasVariable(key)) return false;
            skill.SetVariable(key,value);
            return true;
        }

        public Skill GetSkill(string name)
        {
            Skill s = null;
            if (m_skills.TryGetValue(name, out s)) return s;
            return null;
        }

        public void OnDestroy()
        {
            
        }
    }
}
